﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyBlog.Model;
using MyBlog.DataAccess;
using System.Linq.Expressions;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessCategory : IBusinessCategory
    {
        IDALCategory _dalCategory;
        IUnitOfWork _unitWork;
        public BusinessCategory(IDALCategory dalCategory, IUnitOfWork unitWork)
        {
            _dalCategory = dalCategory;
            _unitWork = unitWork;
        }
        public void Add(CategoryModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Category>(model);
            _dalCategory.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(CategoryModel model)
        {
            var cate = _dalCategory.Get(model.ID);
            cate.CategoryName = model.CategoryName;
            _unitWork.Commit();
        }

        public List<CategoryModel> GetAll(bool isTracking = true)
        {
            var entities = _dalCategory.GetAll(isTracking);
            return Mapper.Map<List<CategoryModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            var entity = _dalCategory.Get(primaryKey);
            entity.Tbl_Article.Clear();
            _dalCategory.Delete(primaryKey);
            _unitWork.Commit();
        }

        public CategoryModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalCategory.Get(primaryKey);
            return Mapper.Map<CategoryModel>(dataEntity);
        }

        public void Delete(CategoryModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Category>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalCategory.Delete(where);
            _unitWork.Commit();
        }

        public CategoryModel Get(CategoryModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Category>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (!string.IsNullOrWhiteSpace(model.CategoryName))
            {
                where = where.And(x => x.CategoryName.ToLower() == model.CategoryName.ToLower());
            }
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                where = where.And(x => x.AccountId.ToLower() == model.AccountId.ToLower());
            }
            var dataEntity = _dalCategory.Get(where);
            return Mapper.Map<CategoryModel>(dataEntity);
        }

        public List<CategoryModel> GetAll(CategoryModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_Category>();
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                where = where.And(x => x.AccountId.ToLower() == model.AccountId.ToLower());
            }
            if (!string.IsNullOrWhiteSpace(model.CategoryName))
            {
                where = where.And(x => x.CategoryName.ToLower() == model.CategoryName.ToLower());
            }
            var entities = _dalCategory.GetAll(where, isTracking);
            return Mapper.Map<List<CategoryModel>>(entities);
        }


        public void AddRange(IEnumerable<CategoryModel> entities)
        {
            throw new NotImplementedException();
        }

        public void DeleteRange(IEnumerable<CategoryModel> entities)
        {
            throw new NotImplementedException();
        }

        public List<CategoryModel> GetAll(CategoryModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }
    }
}
